/*
 * @Description: 
 * @Autor: 赵子惠
 * @Date: 2021-09-03 09:54:41
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:31:52
 */
import { LazyLoadEvent } from 'primeng/api';
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { MessageService } from '@abp/message/message.service';
import { ProductionAssignmentServiceProxy, ProductionReportServiceProxy, ManHourInput, CommonServiceProxy } from '@shared/service-proxies/service-proxies';
import { finalize, filter } from 'rxjs/operators';
import * as moment from 'moment';
import { FileDownloadService } from '@shared/utils/file-download.service';


@Component({
    selector: 'app-person-management',
    templateUrl: './person-management.component.html',
    styleUrls: ['./person-management.component.less']
})
export class PersonManagementComponent extends AppComponentBase {
    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;
    model = undefined;
    modelOption = [];
    lotNo = undefined;
    LotNoList = [];
    person = [];
    personList = [];
    rangeDates: Date[];
    saveFlag = true;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _productionAssignmentServiceProxy: ProductionAssignmentServiceProxy,
        private _productionReportServiceProxy: ProductionReportServiceProxy,
        private _fileDownloadService: FileDownloadService,
        private _commonServiceProxy: CommonServiceProxy,
    ) {
        super(injector);
    }
    ngOnInit() {
        //型号
        this._commonServiceProxy.getModelList(
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.modelOption = result;
            })
    }
    onChange(event) {
        //发次
        this._commonServiceProxy.getLotNoList(
            this.model.value
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.LotNoList = result;
            })
    }
    getTableList(event?: LazyLoadEvent) {
        // if (this.primengTableHelper.shouldResetPaging(event)) {
        //     this.paginator.changePage(0);
        //     return;
        // }
        let personList = []
        if (this.person) {
            this.person.forEach(item => {
                personList.push(item.value)

            })
        }
        // GetStatisticalManHour
        this._productionReportServiceProxy.getStatisticalManHour(
            this.model ? this.model.label : undefined,
            this.lotNo ? this.lotNo.label : undefined,
            this.model ? this.model.value : undefined,
            this.lotNo ? this.lotNo.value : undefined,
            personList,
            undefined,
            this.rangeDates ? moment(this.rangeDates[0]) : undefined,
            this.rangeDates ? moment(this.rangeDates[1]) : undefined,
            this.primengTableHelper.getSorting(this.dataTable),
            this.primengTableHelper.getMaxResultCount(this.paginator, event),
            this.primengTableHelper.getSkipCount(this.paginator, event)
        ).pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
            this.primengTableHelper.totalRecordsCount = result.totalCount;
            this.primengTableHelper.records = result.items;
            this.primengTableHelper.hideLoadingIndicator();
        });
    }
    //获取左侧型号
    getModelList() {
        this._commonServiceProxy.getModelList()
            .pipe(finalize(() => { })).subscribe(result => {
                this.modelOption = result
            })
    }
    //根据型号获取对应的发次
    // getLotNoList(e) {
    //     this.LotNoList = [];
    //     this.lotNo = undefined;
    //     this._commonServiceProxy.getLotNoList()
    //         .pipe(finalize(() => { })).subscribe(result => {
    //             this.LotNoList = result
    //         })
    // }
    //根据型号发次获取对应的人员
    getPersonList(e) {
        this._productionReportServiceProxy.getProductingPerson(this.model.value, e.value.value)
            .pipe(finalize(() => { })).subscribe(result => {
                this.personList = result
            })
    }
    reset() {
        this.model = undefined;
        this.lotNo = undefined;
        this.person = undefined;
        this.rangeDates = []
    }
    outExcel() {
        // if (this.saveFlag) {
        //     this.saveFlag = false;
        let dtp = new ManHourInput();
        this._productionReportServiceProxy.exportPeopleHour(
            dtp)
            .subscribe(result => {
                // this.saveFlag = true;
                // this._fileDownloadService.downloadTempFile(result);
                window.open(result);
                URL.revokeObjectURL(result);
            });
        // }
    }
}
